package com.szqhyxj.youxiangju.biz.receipt.record;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.CustomViewPager;
import com.szqhyxj.youxiangju.biz.merchantHome.MerchantContainerActivity;
import com.szqhyxj.youxiangju.biz.receipt.ReceiptRecordFragmentsContainer;
import com.szqhyxj.youxiangju.biz.receipt.record.fragment.Device.DeviceReceiptRecordFragment;
import com.szqhyxj.youxiangju.biz.receipt.record.fragment.Online.OnlineReceiptRecordFragment;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by leung on 2018/11/26.
 */

public class ReceiptRecordActicirty extends BaseActivity implements ReceiptRecordContract.View, NavigationBar.NavigationBarListener, ReceiptRecordAdapter.ItemClickListener {

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;


    ReceiptRecordPresenter presenter;

    @BindView(R.id.devicearea)
    LinearLayout devicearea;
    @BindView(R.id.onlineeare)
    LinearLayout onlineeare;
    @BindView(R.id.receiptRecord)
    FrameLayout receiptRecord;
    @BindView(R.id.devicebottom)
    View devicebottom;
    @BindView(R.id.onlinebottom)
    View onlinebottom;
    @BindView(R.id.devicetitle)
    TextView devicetitle;
    @BindView(R.id.onlinetitle)
    TextView onlinetitle;
    @BindColor(R.color.invitationColor)
    int invatationColor;
    @BindColor(R.color.linereceipt)
    int gray;
    @BindColor(R.color.black)
    int black;
    @BindView(R.id.navigator)
    LinearLayout navigator;
    @BindView(R.id.container)
    CustomViewPager container;
    private LinearLayoutManager mLinearLayoutManager;
    ReceiptRecordAdapter adapter;

    DeviceReceiptRecordFragment deviceReceiptRecordFragment;
    OnlineReceiptRecordFragment onlineReceiptRecordFragment;
    private List<Fragment> fragments = new ArrayList<>();
    String page;
    private static final String PAGE = "page";

    public static void start(Activity activity, String page) {
        Intent intent = new Intent();
        intent.setClass(activity, ReceiptRecordActicirty.class);
        intent.putExtra(PAGE, page);
        activity.startActivity(intent);

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receipt_record);
        ButterKnife.bind(this);
        presenter = new ReceiptRecordPresenter(this);
        navigationBar.setNavigationBarListener(this);
        page = getIntent().getStringExtra(PAGE);
        presenter.init();
        if (page.equals("device")) {
            presenter.DeviceReceiptFragment();
        }
        if (page.equals("online")) {
            presenter.OnlineReceiptFragment();
        } else {
            presenter.DeviceReceiptFragment();
        }

     /*   FragmentManager fragmentManager = getSupportFragmentManager();
        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        deviceReceiptRecordFragment = DeviceReceiptRecordFragment.newInstance();
        onlineReceiptRecordFragment = OnlineReceiptRecordFragment.newInstance();
        // 先添加再隐藏起来
        transaction.add(R.id.receiptRecord, deviceReceiptRecordFragment);
        transaction.hide(deviceReceiptRecordFragment);
        transaction.add(R.id.receiptRecord, onlineReceiptRecordFragment);
        transaction.hide(onlineReceiptRecordFragment);
        // 默认显示第一个
        transaction.show(deviceReceiptRecordFragment);
        // 提交事务
        transaction.commit();*/

        // swipeLayout.setEnabled(false);
    }

    @OnClick({R.id.devicearea, R.id.onlineeare})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.devicearea:
                //   presenter.setDevice();
                presenter.DeviceReceiptFragment();
             /*   devicebottom.setBackgroundColor(invatationColor);
                onlinebottom.setBackgroundColor(gray);
                devicetitle.setTextColor(invatationColor);
                onlinetitle.setTextColor(black);*/
                break;
            case R.id.onlineeare:
                //   presenter.setOnline();
                presenter.OnlineReceiptFragment();
              /*  devicebottom.setBackgroundColor(gray);
                onlinebottom.setBackgroundColor(invatationColor);
                devicetitle.setTextColor(black);
                onlinetitle.setTextColor(invatationColor);*/
                break;
        }
    }


    @Override
    public void navigationLeft() {
        MerchantContainerActivity.start(this);
    }

    @Override
    public void navigationRight() {

    }

    List<RowsItem> lists = new ArrayList<>();

    @Override
    public void showRecordData(List<RowsItem> list) {
    }

    @Override
    public void onClickItem(OrderCashDetailVO poistionvo, String data) {
    }


    @Override
    public void showLoad(List<RowsItem> list) {
    }

    @Override
    public void showNodata() {

    }

    @Override
    public void enshow() {
    }

    @Override
    public void showDeviceReceiptFragment() {
        // getSupportFragmentManager().beginTransaction().hide(onlineReceiptRecordFragment).show(deviceReceiptRecordFragment).commit();
        container.setCurrentItem(0, false);
        reset();
        devicebottom.setBackgroundColor(invatationColor);
        onlinebottom.setBackgroundColor(gray);
        devicetitle.setTextColor(invatationColor);
        onlinetitle.setTextColor(black);
    }

    @Override
    public void showOnlineReceiptFragment() {
        container.setCurrentItem(1, false);
        reset();
        devicebottom.setBackgroundColor(gray);
        onlinebottom.setBackgroundColor(invatationColor);
        devicetitle.setTextColor(black);
        onlinetitle.setTextColor(invatationColor);
    }

    private void reset() {
        devicebottom.setBackgroundColor(gray);
        onlinebottom.setBackgroundColor(gray);
        devicetitle.setTextColor(black);
        onlinetitle.setTextColor(black);
    }

    @Override
    public void showAll() {
        fragments.clear();
        fragments.add(DeviceReceiptRecordFragment.newInstance());
        fragments.add(OnlineReceiptRecordFragment.newInstance());
        container.setAdapter(new ReceiptRecordFragmentsContainer(getSupportFragmentManager(), fragments));
        container.setOffscreenPageLimit(2);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            MerchantContainerActivity.start(this);
        }
        return super.onKeyDown(keyCode, event);
    }
}